import time
from machine import Pin, Signal

def demo():
    # 初始化一个GPIO引脚，例如GPIO16（继电器）
    pin = Pin(16, mode=Pin.OUT, pull=Pin.PULL_UP)  # 初始化GPIO为输出模式，启用内部上拉电阻
    # 设置初始电平
    pin.value(0)
    # 设置中断处理函数
    def handle_irq(pin):
        print("Interrupt triggered! Pin value is: ", pin.value())
    # 设置中断，触发条件为上升沿和下降沿
    pin.irq(trigger=Pin.IRQ_FALLING | Pin.IRQ_RISING, handler=handle_irq)
    #开关继电器测试
    pin.on()  
    time.sleep(1)
    pin.off()  
    time.sleep(1)
    pin.value(1) 
    time.sleep(1)
    pin.value(0) 
    # 清除中断
    pin.irq(None)
    # 最后设置引脚初始状态
    pin.init(mode=Pin.IN, pull=Pin.PULL_UP)






def demo2():
    # Suppose you have an active-high LED on pin 0
    led1_pin = Pin(38, Pin.OUT)
    # ... and active-low LED on pin 1
    led2_pin = Pin(39, Pin.OUT)

    # Now to light up both of them using Pin class, you'll need to set
    # them to different values
    led1_pin.value(1)
    led2_pin.value(0)

    # Signal class allows to abstract away active-high/active-low
    # difference
    led1 = Signal(led1_pin, invert=False)
    led2 = Signal(led2_pin, invert=True)
    print('------')
    time.sleep(4)
    # Now lighting up them looks the same
    led1.value()
    led2.value()
    time.sleep(1)
    led1.value(0)
    led2.value(0)
    time.sleep(1)
    led1.on()
    led2.on()
    time.sleep(1)
    led1.off()
    led2.off()

if __name__ == '__main__':
    demo()
    demo2()
